package com.sdnu.service.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdnu.common.Result.R;
import com.sdnu.service.entity.quest.CheckInData;
import com.sdnu.service.entity.quest.StudentData;
import com.sdnu.service.entity.quest.vo.CheckInDataQuery;
import com.sdnu.service.entity.quest.vo.CheckInDataVo;
import com.sdnu.service.entity.quest.vo.StudentDataVo;
import com.sdnu.service.service.StudentDataService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Konjacer
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/ucenter/student_data")
@CrossOrigin
public class StudentDataController {

    @Autowired
    private StudentDataService studentDataService;

    @ApiOperation("得到所有学生数据")
    @GetMapping("getAll")
    public R getAll(){
        List<StudentDataVo> list = studentDataService.getAll();
        return R.ok().data("items",list);
    }

    @ApiOperation("添加学生数据")
    @PostMapping("add")
    public R addRecord(@RequestBody StudentData studentData){
        Date admissionDate = studentData.getAdmissionDate();
        admissionDate.setDate(admissionDate.getDate()+1);
        studentDataService.save(studentData);
        //日志
        System.out.println(new Date()+":"+studentData.getName()+"被添加了");
        return R.ok();
    }

    @ApiOperation("通过id修改学生数据")
    @PutMapping("update")
    public R updateRecord(@RequestBody StudentData studentData){
        boolean b = studentDataService.updateById(studentData);
        return b?R.ok():R.error();
    }

    @ApiOperation("通过id删除学生数据")
    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable String id){
//        boolean b = studentDataService.updateById(studentDataService.getById(id).setIsDeleted(1));
        boolean b = studentDataService.removeById(id);
        return b?R.ok():R.error();
    }

    @ApiOperation("通过id获取学生数据")
    @GetMapping("get/{id}")
    public R getRecordById(@PathVariable String id){
        QueryWrapper<StudentData> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        wrapper.eq("is_deleted",0);
        StudentData item = studentDataService.getOne(wrapper);
        return R.ok().data("item",item);
    }

    @ApiOperation("分页查询学生数据")
    @PostMapping("pageSelect/{current}/{limit}")
    public R pageSelect(
            @ApiParam(name = "current", value = "当前页码", required = true)
            @PathVariable long current,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable long limit,

            @ApiParam("查询条件")
            @RequestBody StudentDataVo studentDataVo
    ){
        Page<StudentData> page = new Page<>(current, limit);
//        得到构造条件
        QueryWrapper<StudentData> wrapper = studentDataService.querySelect(studentDataVo);
        wrapper.orderByDesc("gmt_create");
        wrapper.eq("is_deleted",0);

        List<StudentDataVo> finalList = studentDataService.pageSelect(page, wrapper);

        //经过处理，原始数据封装在page中
        //total是所有的记录数，不是records中的记录数
        long total = page.getTotal();

        return R.ok().data("total",total).data("items",finalList);
    }

}

